// 防抖
export const debounce = (fu: Function, t: number) => {
  const delay = t || 500
  let timer: ReturnType<typeof setTimeout> | null = null
  return function (this: any, ...args: any[]) {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fu.apply(this, args)
    }, delay)
  }
}
// 节流
export const throttle = (fn: (...args: any[]) => void, t: number) => {
  let last: number | null = null
  let timer: ReturnType<typeof setTimeout> | null = null
  const interval = t || 500
  return function (this: any, ...args: any[]) {
    const now = Date.now()
    if (last !== null && now - last < interval) {
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        last = now
        fn.apply(this, args)
      }, interval)
    } else {
      last = now
      fn.apply(this, args)
    }
  }
}
